/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.treeeditpart.diagramview;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.uml2.uml.NamedElement;

import com.cea.papyrus.core.actions.CloseDiagramAction;
import com.cea.papyrus.core.actions.OpenDiagramAction;
import com.cea.papyrus.core.actions.delete.DeleteFromModelAction;
import com.cea.papyrus.core.commands.diagram.CloseDiagramCommand;
import com.cea.papyrus.core.commands.diagram.DiagramDeleteCommand;
import com.cea.papyrus.core.commands.diagram.OpenDiagramCommand;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editor.MultiDiagramEditor;
import com.cea.papyrus.core.editpart.IUMLDiagramElementEditPart;
import com.cea.papyrus.core.listener.DiagramListener;
import com.cea.papyrus.core.treeeditpart.CommonTreeEditPart;
import com.cea.papyrus.core.ui.properties.advanced.EObjectPropertySource;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * this editpart is used to display the diagram in the list 
 */
public class DiagramItemEditPart extends CommonTreeEditPart
implements  IUMLDiagramElementEditPart {

	/**
	 * the reference to the digram element
	 */
	private Diagram diagram= null;

	private IPropertySource propertySource;

	/**
	 * the listener
	 */
	private DiagramListener listener=null;

	/**
	 * Create a new instance of this edit part using the given model element.
	 * @param element a non-null ShapesDiagram instance
	 * @param modelManager 
	 */
	public DiagramItemEditPart(Diagram element,ModelManager modelManager) {
		super(element, modelManager);
		this.diagram = element;
		listener= new DiagramListener(this,modelManager);
	}

	/**
	 * {@inheritDoc}
	 */

	public String getText() {
		String out= super.getText();
		out=out+ " ["+((NamedElement)((Uml1SemanticModelBridge) getDiagram().getOwner()).getElement()).getQualifiedName()+"]";
		return out;
	}

	/**
	 * this method is us to know the associated diagram
	 * @return the Diagram associated to this treeEditPart
	 */
	public Diagram getDiagram(){
		return diagram;
	}

	/**
	 * {@inheritDoc}
	 */
	public Object getAdapter(Class key) {
		if (IPropertySource.class == key) {
			return getPropertySource();
		}
		return super.getAdapter( key );
	}

	@Override
	public void activate() {
		if (!isActive()) {
			super.activate();
			// ad the listener
			((Notifier)getDiagram()).eAdapters().add(listener);
		}
	}

	@Override
	public void deactivate() {
		if (isActive()) {
			super.deactivate();
			// remove the listener
			((Notifier)getDiagram()).eAdapters().remove(listener);
		}
	}
	/**
	 *{@inheritDoc} 
	 */
	protected IPropertySource getPropertySource() {
		if( propertySource == null ) {
			propertySource = new EObjectPropertySource( this.getDiagram());
		}
		return propertySource;
	}

	/**
	 * 
	 *{@inheritDoc}
	 */
	@Override
	public Command getCommand(Request request) {
		Diagram diagram = getDiagram();
		CompoundCommand command = new CompoundCommand();
		if(DeleteFromModelAction.DELETE_FROM_MODEL_ACTION_REQUEST.equals(request.getType())) {
			command.add(new DiagramDeleteCommand(diagram,this.getModelManager() ));
			return command;
		}
		if(CloseDiagramAction.CLOSE_DIAGRAM_ACTION_REQUEST.equals(request.getType())) {
			command.add(new CloseDiagramCommand(diagram));
			return command;
		}
		if(OpenDiagramAction.OPEN_DIAGRAM_ACTION_REQUEST.equals(request.getType())) {
			// check if it is already open.
			// if it is not open, open it
			// If it is already open, just make it visible in the editor
			if(!diagram.isIsVisible()) {
				command.add(new OpenDiagramCommand(diagram));
			} else {
				IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
				if(part instanceof MultiDiagramEditor) {
					// try to find the corresponding editor
					((MultiDiagramEditor)part).setActiveEditor(diagram);
				}
			}
			return command;
		}
		return super.getCommand(request);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void performRequest(Request req) {
		final Command command = getCommand(req);
		if(command !=null) {
			BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {
				/* (non-Javadoc)
				 * @see java.lang.Runnable#run()
				 */
				public void run() {
					
					CommandStack stack=getViewer().getEditDomain().getCommandStack();
					stack.execute(command);
				}
			});

		}
	}

}


